﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAPbouiCOM;
using SAPbobsCOM;

namespace BoAddonBiz_FreightForwarder.Land
{
    [BoAddonBiz_FreightForwarder.BusinessBase.ControlB1Form(IsNew = true, FormType = B1FormTypes.ado_AVA_LN_Insure)]
    public class B1Form_Insure_Land : B1Form_BaseOperationSon
    {
        public override string ThisSubTableName
        {
            get { return null; }
        }
        public override string ThisSubTableName2
        {
            get { return null; }
        }
        public override string FatherField
        {
            get { return "U_IsInsure"; }
        }
        public override string FatherUDS
        {
            get { return "OISU_UID"; }
        }
        public override string ThisTableName
        {
            get { return "@AVA_LN_OISU"; }
        }
        public override string ThisObjectCode
        {
            get { return "AVA_LN_Insure"; }
        }
        public B1Form_Insure_Land()
            : base()
        {
            this.frm_ChooseFormListEvent += new SAPBoAddon.IB1FormEvents.B1ChooseFormListEventEventHandler(B1Form_Insure_frm_ChooseFormListEvent);
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_Insure_frm_ItemEvent);
        }

        void B1Form_Insure_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            { }
            else
            {
                if ((pVal.EventType == BoEventTypes.et_LOST_FOCUS && pVal.ItemUID == "52") || (pVal.EventType == BoEventTypes.et_LOST_FOCUS && pVal.ItemUID == "49"))
                {
                    CalculationAmountInsured();
                }

            }
        }

        #region 事件处理
        void B1Form_Insure_frm_ChooseFormListEvent(SAPBoAddon.B1AddonBase.B1ChooseFormList pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {
                SAPbouiCOM.ChooseFromList cfl = this.CurrentForm.ChooseFromLists.Item(pVal.ChooseFromListUID);
                SAPbouiCOM.Conditions cons;
                SAPbouiCOM.Condition con;
                switch (pVal.ChooseFromListUID)
                {
                    case "CFL_CardCode"://委托单位
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup13";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "validFor";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    case "CFL_Company"://保险公司名称
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup1";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "validFor";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                }
            }
            else
            {
                if (pVal.Action_Success)
                {
                    SAPbouiCOM.DBDataSource dbds = this.CurrentForm.DataSources.DBDataSources.Item(ThisTableName);
                    if (pVal.ChooseFromListUID == "CFL_CardCode")//客户
                    {
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item("@AVA_LN_OISU");
                        dbds.SetValue("U_CardCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_CardName", dbds.Offset, pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                        //dbds.SetValue("U_CntctCode", dbds.Offset, pVal.SelectedObjects.GetValue("CntctPrsn", 0).ToString());
                        dynamic Cntct = Utils.GetDefaultContractPerson(this.SBOCompany, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        if (Cntct != null)
                        {
                            dbds.SetValue("U_CntctCode", dbds.Offset, Cntct.FullName);
                        }
                    }
                    else if (pVal.ChooseFromListUID == "CFL_Company")//客户
                    {
                        dbds.SetValue("U_InsuranceCoCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_InsuranceCoName", dbds.Offset, string.IsNullOrEmpty(pVal.SelectedObjects.GetValue("CardName", 0).ToString()) ? pVal.SelectedObjects.GetValue("CardFName", 0).ToString() : pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                    }
                }
            }
        }
        #endregion

        #region  继承至父类

        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            FormSetting.FormType = B1FormTypes.ado_AVA_LN_Insure;
            FormSetting.FormFileName = "AVA_LN_InsureForm.srf";
            base.FormInfoSetting(FormSetting);
        }

        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {
            base.InitializeForm(InitPar);
            Form listForm = SBOApp.Forms.Item(InitPar.FatherFormUID.Split(';')[0]);
            Form BNOForm = SBOApp.Forms.Item(listForm.DataSources.UserDataSources.Item("ParentUID").Value);
            //this.CurrentForm.DataSources.DBDataSources.Item(ThisTableName).SetValue("U_SailingDate", 0, BNOForm.DataSources.DBDataSources.Item("@AVA_LN_OBNO").GetValue("U_ATD", 0));
            Utils.InitialItemComboBox(this.SBOCompany, this.CurrentForm.Items.Item("54"), "DocCurrCod", "CurrName", "OCRN", null, null, null, true);

            this.CurrentForm.DataSources.DBDataSources.Item(ThisTableName).SetValue("U_CargoValue", 0, BNOForm.DataSources.DBDataSources.Item("@AVA_LN_OBNO").GetValue("U_CargoValue", 0));
            this.CurrentForm.DataSources.DBDataSources.Item(ThisTableName).SetValue("U_Currency", 0, BNOForm.DataSources.DBDataSources.Item("@AVA_LN_OBNO").GetValue("U_CargoValueUnit", 0));

            //TotalCargoValue(InitPar);
        }
        #endregion

        /// <summary>
        /// 计算货物详情中的货值总和
        /// </summary>
        /// <param name="InitPar"></param>
        private void TotalCargoValue(SAPBoAddon.B1FormInitializePar InitPar)
        {
            string fatherFormUid = SBOAddonBase.Forms.Item(InitPar.FatherFormUID.Split(';')[0]).DataSources.UserDataSources.Item("ParentUID").Value.ToString().Trim();
            SAPbouiCOM.DBDataSource dbds = SBOAddonBase.Forms.Item(fatherFormUid).DataSources.DBDataSources.Item("@AVA_LN_BNO1");
            SAPbouiCOM.ComboBox comBox = this.CurrentForm.Items.Item("54").Specific;
            decimal CargoValueTotal = 0;
            for (int i = 0; i < dbds.Size; i++)
            {
                if (!string.IsNullOrEmpty(dbds.GetValue("U_CargoValue", i).ToString().Trim()))
                    CargoValueTotal += Convert.ToDecimal(dbds.GetValue("U_CargoValue", i).ToString().Trim());
            }
            this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName).SetValue("U_CargoValue", 0, CargoValueTotal.ToString());
            //SAPbouiCOM.EditText oEdit = this.CurrentForm.Items.Item("49").Specific;
            //oEdit.Value = CargoValueTotal.ToString().Trim();
            SAPbouiCOM.Matrix oMtx = SBOAddonBase.Forms.Item(fatherFormUid).Items.Item("mtx_0").Specific;
            if (oMtx.RowCount > 0)
                if (!string.IsNullOrEmpty(oMtx.Columns.Item("col_19").Cells.Item(1).Specific.Value.ToString().Trim()))
                    comBox.Select(oMtx.Columns.Item("col_19").Cells.Item(1).Specific.Value.ToString(), BoSearchKey.psk_ByValue);
        }
        /// <summary>
        /// 计算投保金额
        /// </summary>
        private void CalculationAmountInsured()
        {
            string percentValue = SAPBoAddon.B1Assistant.Item.GetValue(this.CurrentForm, "52").ToString().Trim();
            string CargoValue = SAPBoAddon.B1Assistant.Item.GetValue(this.CurrentForm, "49").ToString().Trim();
            if (!string.IsNullOrEmpty(percentValue) && !string.IsNullOrEmpty(CargoValue))
            {
                decimal DPercentValue = Convert.ToDecimal(percentValue);
                decimal DCargoValue = Convert.ToDecimal(CargoValue);
                DPercentValue = 1 + DPercentValue / 100;
                decimal AmountInsured = DCargoValue * DPercentValue;
                SAPbouiCOM.EditText oEditText = this.CurrentForm.Items.Item("53").Specific;
                oEditText.Value = AmountInsured.ToString().Trim();
            }
        }
    }
}
